'''
A very simple URLSupplier implementaion ,for test use,store all in url in memory.-:)
@author Devdoer<devdoer@gmail.com>

'''
import threading
from anole.utils import urlloader
from anole import AnoleError
class FrontierError(AnoleError):pass
class EGetUrl(FrontierError):
	WAIT=0
	RETRY=1

class UrlItem:
	def __init__(self,entry,**keyargs):
		self._entry=entry
		for key,val in keyargs.iteritems():
			setattr(self,key,val)
	def to_entry(self):
		return self._entry[:]

			
class SimpleUrlSupplier:
        def __init__(self,loader):
                self.__urls=loader.load_urls()
		self.__U=loader.UrlItem
                self._lock=threading.Lock()
        def get_url(self):
		UrlItem=self.__U
		self._lock.acquire()
		try:
			entry=self.__urls.pop(0)
		except IndexError:
			self._lock.release()
			raise EGetUrl(EGetUrl.WAIT,10)
		else:
			self._lock.release()
			return UrlItem(entry)
	def put_url(self,uItem):
                self._lock.acquire()
		self.__urls.append(uItem.to_entry())
		self._lock.release()
		
                        
